/**
 * @file
 */
 
#ifndef WII_GI_C_TEXTURE_H
#define WII_GI_C_TEXTURE_H

#include "c_Engine.h"


namespace wiigi
{
   class c_Texture
   {
      public:
         
         struct s_2D_TextCoords
         {
            f32 s1, t1, s2, t2;
         };
      
      private:
         static const u32 d_Signature = 0xDEAD0110;
         u32  m_Signature;    
         
         static const f32 d_FRAME_CORR = 0.001f;
         
      public:
         c_Texture( void );
         ~c_Texture();
      
         e_Status createFromBuffer( 
            const unsigned char Buffer[]
         );
        
         void getFullFrame( 
            s_2D_TextCoords& Frame
         );
         
         void getSubFrame( 
            s_2D_TextCoords& TextureFrame,
            u32              FrameNumber,
            c_u32Rect&       FrameSize
         );
         
         void getSubFrame( 
            s_2D_TextCoords& TextureFrame,
             c_u32Rect&       FrameSize
         );

      public:
         /**
          * lite weitht run time object validation
          *
          * @param  Texture Ptr to texture class to validate
          * @retval bool    true if supplied class is a valid texture class, else false
          */
         bool checkSignature( c_Texture* Texture ) const
         {
            return (Texture->m_Signature == d_Signature) ? true : false;
         }
         
         /**
          * sets this texture as the current one
          */
         void setAsCurrent( void )
         {
            GX_LoadTexObj(&m_GX_TexObj, GX_TEXMAP0);
         }
         
         /**
          * returns pixel width of this texture
          *
          * @retval unsigned int width in pixels
          */
         unsigned int getWidth( void ) const
         {
            return m_Width;
         }
         
         /**
          * returns pixel height of this texture
          *
          * @retval unsigned int height in pixels
          */
         unsigned int getHeight( void ) const
         {
            return m_Height;
         }
         
         /**
          * sets the supplied rectagle to the size of this texture
          *
          * @param SizeRect
          */
         void getSizeRect( c_u32Rect& SizeRect )
         {
            SizeRect.setValues( 0, 0, m_Height, m_Width );
            
            return;
         }
         
         /**
          * sets the supplied rectagle to the size of this texture
          *
          * @param SizeRect
          */
         void getSizeRect( c_u32Rect* SizeRect )
         {
            getSizeRect( *SizeRect );
            
            return;
         }
         
         /**
          * flush texture contents from CPU data cache
          */
         void flushData( void )
         {
            const u32 Size = m_Width * m_Height * 4;
            
            if( m_Data && Size )
            {
               DCFlushRange(m_Data, Size );
            }
            
            return;
         }
         
      protected:
         GXTexObj      m_GX_TexObj;
         
         unsigned int  m_Width;
         unsigned int  m_Height;
         void*         m_Data;
         
   };
   
}

#endif //WII_GI_C_TEXTURE_H
